package com.cdm.beans.user;

import java.util.List;

import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import net.sf.frails.JSFHelper;
import net.sf.frails.aop.annotations.InjectBean;
import net.sf.frails.bean.annotations.DefMbean;
import net.sf.frails.bean.annotations.InVar;
import net.sf.frails.bean.annotations.PreRender;
import net.sf.frails.bean.annotations.Prop;
import net.sf.frails.utils.FacesMessageUtils;

import com.cdm.domain.dao.ContentDAO;
import com.cdm.domain.dao.UserDAO;
import com.cdm.domain.service.Service;
import com.cdm.domain.service.ServiceImpl;
import com.cdm.persistence.Content;
import com.cdm.persistence.ContentType;
import com.cdm.persistence.EntityManagerHelper;
import com.cdm.persistence.User;
import com.cdm.util.Constants;
import com.vivo.util.DTool;

@DefMbean
public class SpBean {

	private static final String thisPage = "/user/sp";

	@Prop
	User loginUser;

	/*
	 * Prepared List
	 */
	@Prop
	SelectItemGroup roleList;

	@Prop(readOnly = true)
	List<User> cps;

	public SelectItemGroup getCpItems() {
		SelectItem[] items = new SelectItem[cps.size()];
		for (int i = 0; i < cps.size(); i++) {
			items[i] = new SelectItem(cps.get(i), cps.get(i).getUserName());
		}
		SelectItemGroup group = new SelectItemGroup();
		group.setSelectItems(items);
		return group;
	}

	@Prop(readOnly = true)
	List<ContentType> contentTypeList;

	public SelectItemGroup getContentTypeList() {
		int size = contentTypeList.size();
		SelectItem[] items = new SelectItem[size];
		for (int i = 0; i < size; i++) {
			ContentType type = contentTypeList.get(i);
			items[i] = new SelectItem(type, type.getName());
		}
		SelectItemGroup group = new SelectItemGroup();
		group.setSelectItems(items);
		return group;
	}

	@InVar
	@Prop
	User user;

	@Prop
	List<Content> contents;
	@Prop
	List<Content> contentsDecrypted;

	@Prop
	boolean create = true;

	@SuppressWarnings("unchecked")
	@PreRender(failOutcome = thisPage)
	public boolean init() {
		Object object = JSFHelper.getFromApplication(Constants.LOGIN_USER_KEY);
		if (object instanceof User && object != null) {
			loginUser = (User) object;
		}
		if (create == true) {
			// BUG
			this.contents = EntityManagerHelper.getEntityManager().createNativeQuery(
			        "select * from cdm_content co where co.dcid not in("
			                + "select c.dcid from cdm_content c left join cdm_content_use u on c.dcid =  u.dcid where u.spid=?)",
			        Content.class).setParameter(1, loginUser.getUserId()).getResultList();

			this.contentsDecrypted = EntityManagerHelper.getEntityManager().createNativeQuery(
			        "select * from cdm_content c left join cdm_content_use u on c.dcid =  u.dcid where u.spid=?", Content.class)
			        .setParameter(1, loginUser.getUserId()).getResultList();

			this.cps = service.getAuthedUsersByRole(User.ROLE_CP);
			contentTypeList = service.getAllContentTypes();
			// create = false;
		}
		if (null == roleList) {
			roleList = new SelectItemGroup();
			roleList.setSelectItems(new SelectItem[] { new SelectItem(0, FacesMessageUtils.getInstance().getMessage("label_user_role_cp")),
			        new SelectItem(1, FacesMessageUtils.getInstance().getMessage("label_user_role_sp")),
			        new SelectItem(2, FacesMessageUtils.getInstance().getMessage("label_user_role_cdm")),
			        new SelectItem(3, FacesMessageUtils.getInstance().getMessage("label_user_role_admin")),
			        new SelectItem(4, FacesMessageUtils.getInstance().getMessage("label_user_role_enduser")) });
		}

		DTool.d.debug("Enter Admins Home Page");
		return true;
	}

	@InjectBean
	public String auth() {
		service.auth(user);
		create = true;
		return thisPage;
	}

	@InjectBean
	public String delete() {
		service.delete(user);
		create = true;
		return thisPage;
	}

	@InjectBean
	public String detail() {
		service.delete(user);
		create = true;
		return thisPage;
	}
	
	public String contentInfo() {
		JSFHelper.setExpressionValue("contentInfo.contents", contentDAO.findByProperty(UserDAO.USER_ID, loginUser.getUserId()));
		return "contentInfo";
	}

	private ContentDAO contentDAO = new ContentDAO();
	private UserDAO userDAO = new UserDAO();
	private Service service = new ServiceImpl();
}
